package itsm.isperp.module.utils;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.module.dto.bpm.BpmRelationProcessMapDTO;
import itsm.isperp.module.entity.bpm.BpmProcessRelation;
import itsm.isperp.module.entity.incident.IcdIncident;
import itsm.isperp.module.entity.knowledge.KngKnowledgeItem;
import itsm.isperp.module.service.bpm.BpmProcessRelationService;
import itsm.isperp.module.service.bpm.BpmRelationProcessMapService;
import itsm.isperp.module.service.incident.IcdIncidentService;
import itsm.isperp.module.service.knowledge.KngKnowledgeItemService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2016-4-25
 * @since 1.0
 * 
 */
public class ProcessRelationUtils {
	
	
	//更新处理人员、满意度
	public static void updateIncidentColumnValue(String id)
	{
		IcdIncidentService icdIncidentService = ContextHolder
				.getSpringBean("icdIncidentService");
		IcdIncident entity=icdIncidentService.findOne(id);
		//根据事件分类查找关联表
		String catId=entity.getIcdCategoryId();
		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		String sql1="select db_name from icd_category_property_setting where category_id='"+catId+"'";
		String tableName=jdbcTemplate.queryForObject(sql1, String.class);
		if(StringUtils.isNotEmpty(tableName))
		{
			switch(tableName)
			{
			case "incident_jszc"://技术支持
				String updateSql1="UPDATE icd_incident a INNER JOIN  incident_jszc b on a.id=b.relation_id SET a.satisfaction=b.jszc26p,a.supporter_name=ifnull(b.jszc14p,b.jszc17p) WHERE a.id='"+id+"'";
				jdbcTemplate.execute(updateSql1);
				String updateSql2="update icd_incident a set solver_id=supporter_id,solver_name=supporter_name,supporter_id=(SELECT NAME FROM app_user WHERE fullname=a.supporter_name) where a.id='"+id+"'";
				jdbcTemplate.execute(updateSql2);
				break;
			case "2":
				break;
			case "3":
				break;
			}
		}
	}
	
	public static void insertBackOpion(String taskId)
	{
	  String sql="select * from bpm_action_history where task_id='"+taskId+"'";
	  JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
	  List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
	  if(list.size()>0)
	  {
	    String processId=list.get(0).get("process_id").toString();
	    String actionName=list.get(0).get("action_name").toString();
	    String opions=list.get(0).get("comment").toString();
	    if(StringUtils.isNoneEmpty(opions) && actionName.equals("退回重新处理"))
	    {
	      String sql1="select * from icd_incident where process_id='"+processId+"'";
	      List<Map<String,Object>> list2=jdbcTemplate.queryForList(sql1);
	      if(list2.size()>0)
	      {
	        String id=list2.get(0).get("id").toString();
	        String sql3="update icd_incident set back_opnion=? where id=?";
	        jdbcTemplate.update(sql3,new Object[]{opions,id});
	      }
	    }
	    
	  }
	  
	  
	}

	public static void startNewProcess(String fromId, String fromProcessKey,
			String processKey) throws SQLException, TemplateException {

		BpmRelationProcessMapService bpmRelationProcessMapService = ContextHolder
				.getSpringBean("bpmRelationProcessMapService");

		BpmRelationProcessMapDTO[] dtos = bpmRelationProcessMapService
				.getRelationMap(fromId, fromProcessKey, processKey);

		String id = null;
		String title = null;
		// BpmProcessRunService bpmProcessRunService = ContextHolder
		// .getSpringBean("bpmProcessRunService");

		BpmProcessRelationService bpmProcessRelationService = ContextHolder
				.getSpringBean("bpmProcessRelationService");
		switch (processKey) {

		case "knowledgeProcess":

			KngKnowledgeItemService kngKnowledgeItemService = ContextHolder
					.getSpringBean("kngKnowledgeItemService");
			KngKnowledgeItem entity = new KngKnowledgeItem();

			for (BpmRelationProcessMapDTO d : dtos) {
				try {
					org.apache.commons.beanutils.BeanUtils.copyProperty(entity,
							d.getField(), d.getValue());
				} catch (Exception e) {

				}
			}
			entity.setStatus("新建");
			kngKnowledgeItemService.saveOrUpdate(entity);

			id = entity.getId();
			title = "自动发起知识";

			break;

		}

		BpmProcessRelation relation = new BpmProcessRelation();
		relation.setFromId(fromId);
		relation.setFromType(fromProcessKey);
		relation.setToId(id);
		relation.setToType(processKey);

		bpmProcessRelationService.saveOrUpdate(relation);

		// bpmProcessRunService.startProcess(processKey, id, title);

	}
}
